import Mock from 'mockjs'
import { param2Obj } from '../utils/myUtils'
Mock.Random.extend({
    phone: function() {
        var phonePrefixs = ['132', '135', '189'] // 自己写前缀哈
        return this.pick(phonePrefixs) + Mock.mock(/\d{8}/) //Number()
    }
})
var Arr = []
for (let i = 0; i < 50; i++) {
    Arr.push({
        id: Mock.mock('@guid'),
        position: Mock.mock('@region'),
        address: Mock.mock('@county(true)') + ' ' + Mock.mock('@cword(2, 2)') + '路' + Mock.mock('@natural(1, 100)') + '号',
        phone1: Mock.Random.phone(),
        phone2: Mock.Random.phone(),
        bus: '乘坐' + Mock.mock('@natural(1, 10)') + ',' + Mock.mock('@natural(10, 20)') + ',' + ',' + Mock.mock('@natural(20, 30)') + ',' + ',' + Mock.mock('@natural(30, 40)') + '路公交车可直达',
        time: Mock.mock('@datetime()'),

    })
}
export default {
    hospData: (config) => {
        let { inputText, page, pageSize } = param2Obj(config.url)
        let res = []
        if (inputText) {
            res = Arr.filter(item => {
                return item.position.indexOf(inputText) != -1
            })
        } else {
            res = Arr
        }
        //分页
        var newRes = []
        newRes = res.filter((item, index) => {
            return (page - 1) * pageSize <= index && index < page * pageSize
        })
        return {
            code: 200,
            msg: '测试',
            data: newRes,
            total: res.length
        }
    },
    deleteData: config => {
        const { id } = JSON.parse(config.body)
        Arr.forEach((item, index) => {
            if (item.id == id) {
                Arr.splice(index, 1)
            }
        })
        return {
            code: 200,
            msg: '测试',
            data: null
        }
    },
    deleteAll: config => {
        const { ids } = JSON.parse(config.body)
            // console.log(ids)
        var res = Arr.filter(item => {
            return ids.indexOf(item.id) == -1
        })
        Arr = res
        return {
            code: 200,
            msg: '测试',
            data: null
        }
    },
    addArr: config => {
        const { position, address, phone1, phone2, bus } = JSON.parse(config.body)
        Arr.unshift({
            id: Mock.mock('@guid'),
            position: position,
            address: address,
            phone1: phone1,
            phone2: phone2,
            bus: bus,
            time: Mock.mock('@datetime()'),
        })
        return {
            code: 200,
            msg: '测试添加',
            data: null
        }
    },
    upDateArr: config => {
        console.log(config)
        const { id, position, address, phone1, phone2, bus } = JSON.parse(config.body)
        Arr.forEach(item => {
            if (item.id == id) {
                item.position = position
                item.address = address
                item.phone1 = phone1
                item.phone2 = phone2
                item.bus = bus
            }
        })
        return {
            code: 200,
            msg: '修改测试',
            data: null
        }
    }
}